package com.example.seckilldemo.controller;

import com.example.seckilldemo.pojo.User;
import com.example.seckilldemo.service.IGoodsService;
import com.example.seckilldemo.service.IUserService;
import com.example.seckilldemo.vo.GoodsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Author Wangbaba
 * @Date 2024/3/18 18:53
 * @Version 1.0
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;

    @RequestMapping(value = "/list",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toList(Model model, User user,HttpServletRequest request,HttpServletResponse response) {
//        //redis中获取页面，如果不为空，直接返回页面
        ValueOperations valueOperations=redisTemplate.opsForValue();
        String html= (String) valueOperations.get("goodsList");
        if (!StringUtils.isEmpty(html)){
            return html;
        }
        model.addAttribute("user", user);
        model.addAttribute("goodsList", goodsService.findGoodsVo());
        WebContext webContext=new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        html=thymeleafViewResolver.getTemplateEngine().process("goodsList",webContext);
        if (!StringUtils.isEmpty(html)){
            valueOperations.set("goodsList",html,60, TimeUnit.SECONDS);
        }
        return html;
    }

    @RequestMapping(value = "/toDetail",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toDetail(Long goodsId, User user, Model model,HttpServletRequest request,HttpServletResponse response) {
        ValueOperations valueOperations=redisTemplate.opsForValue();
        String html= (String) valueOperations.get("goodsDetail:"+goodsId);
        if (StringUtils.isNotEmpty(html)){
            return html;
        }
        GoodsVo goodsVo = goodsService.findGoodsById(goodsId);
        Date nowDate = new Date();
        //未开始
        int seckillStatus = 0;
        //秒杀倒计时
        int remainSeconds = 0;
        if (nowDate.before(goodsVo.getStartDate())) {
            remainSeconds=((int)((goodsVo.getStartDate().getTime())-nowDate.getTime())/1000);
        } else if (nowDate.after(goodsVo.getEndDate())) {
            //已结束
            seckillStatus = 2;
            remainSeconds = -1;
        } else {
            //进行中
            seckillStatus = 1;
        }
        model.addAttribute("user", user);
        model.addAttribute("goods", goodsVo);
        model.addAttribute("seckillStatus", seckillStatus);
        model.addAttribute("remainSeconds",remainSeconds);
        WebContext webContext=new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        html=thymeleafViewResolver.getTemplateEngine().process("goodsDetail",webContext);
        if (!StringUtils.isEmpty(html)){
            valueOperations.set("goodsDetail:"+goodsId,html,60, TimeUnit.SECONDS);
        }
        return html;
    }
}
